/*
 * KeyValueCodingCheckbox.java
 *
 * Created on November 12, 2006, 11:46 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package edu.umn.cs5115.scheduler.framework.gui;

import edu.umn.cs5115.scheduler.framework.KeyValueCodingBase;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;

/**
 * Adds key-value coding compliance to checkboxes.
 * @author grant
 */
public class KeyValueCodingCheckbox extends KeyValueCodingBase
{
    /** Key that selection notifications will be sent out on (whenever the user checks or unchecks the box.) */
    public final static String SELECTION_KEY = "selection";
    /** The checkbox we watch. */
    private Button checkbox;
    /** The last value of the checkbox's selection. */
    private boolean selected;
    
    /** Listens for checkbox selection. */
    private Listener checkListener = new Listener()
    {
        public void handleEvent(Event event)
        {
            selected = checkbox.getSelection();
            didChangeValueForKey(SELECTION_KEY);
        }
    };
    
    /**
     * Creates a new instance of KeyValueCodingCheckbox.  This creates a new 
     * checkbox in the specified parent component.
     */
    public KeyValueCodingCheckbox(Composite parent)
    {
        checkbox = new Button(parent, SWT.CHECK);
        checkbox.setSize(checkbox.computeSize(SWT.DEFAULT, SWT.DEFAULT));
        
        checkbox.addListener(SWT.Selection, checkListener);
    }
    
    /**
     * Shows or hides the checkbox.
     * @param b True to hide the checkbox, false to show it.
     */
    public void setHidden(boolean b)
    {
        checkbox.setVisible(!b);
    }
    
    /**
     * Is the checkbox visible or hidden?
     * @return true if the checkbox is hidden, false if it is visible.
     */
    public boolean isHidden()
    {
        return !checkbox.isVisible();
    }
    
    /**
     * Destroys the checkbox.
     */
    public void dispose()
    {
        checkbox.removeListener(SWT.Selection, checkListener);
        checkbox.dispose();
        checkbox = null;
    }
    
    /**
     * Get the current selection.
     * @return True if the checkbox is checked, false if it is not.
     */
    public boolean getSelection()
    {
        return selected;
    }
    
    /**
     * Set the selection.
     * @param selected True to check the checkbox, false to uncheck it.
     */
    public void setSelection(boolean selected)
    {
        if (selected != this.selected)
        {
            this.selected = selected;
            checkbox.setSelection(selected);
            didChangeValueForKey(SELECTION_KEY);
        }
    }

    /**
     * Get the size of the checkbox.
     * @return The size of the checkbox.
     * @see Button#getSize()
     */
    public Point getSize()
    {
        return checkbox.getSize();
    }

    /**
     * Set the location of the checkbox.
     * @param x The x-coordinate of the upper left corner of the checkbox.
     * @param y The y-coordinate of the upper left corner of the checkbox.
     */
    public void setLocation(int x, int y)
    {
        checkbox.setLocation(x, y);
    }
}
